<?php
/**
* 项目对象
* @author zhufu 2014-06-06
*/
class project extends admin{
	private $table_project = 'syt_project'; //项目表
	private $table_admin = 'syt_admin'; //后台管理员表
	private $table_project_bind = 'syt_bind'; //项目设备绑定表
	private $table_device = 'syt_device'; //设备表
	private $table_project_address = 'syt_project_address'; //项目作业地址表
	private $table_record = 'syt_checkin'; //记录

	/**
	* 获取表
	*
	* @param mixed $table_name
	*/
	public function get_table_name($table_name){
		return $this->$table_name;
	}



	/**
	* 根据项目ID，得到项目的信息
	* @author zhufu 2014-06-06
	*
	* @param int $project_id    项目的编号ID
	*/
	public function info($project_id,$cols='*'){
		if(!is_numeric($project_id)){
			return false;
		}
		$sql = "SELECT {$cols} FROM {$this->table_project} WHERE id={$project_id}";
		return $this->_mysql(0)->findone($sql);
	}

	/**
	* 根据ID获取项目作业点
	*
	* @param mixed $id
	* @return mysql对象
	*/
	function address_info($id){
		if(!is_numeric($id)){
			return false;
		}
		$sql = "SELECT * FROM {$this->table_project_address} WHERE id={$id}";
		return $this->_mysql(0)->findone($sql);
	}
	/**
	* 获取项目列表
	*
	* @param mixed $data
	* @param mixed $page
	* @param mixed $pagesize
	* @return mysql对象
	*/
	public function get_project_list($data,$page=1,$pagesize=10){
		$sql = "SELECT a.*,b.name as bname FROM {$this->table_project} as a left join {$this->table_admin} as b on a.uid=b.id WHERE 1";
		if($data['title']){
			$sql.=" And (a.title like '%{$data['title']}%' or b.name like '%{$data['title']}%')";
		}
		if($data['state']>0){
			$sql.=" And a.state={$data['state']}";
		}


		$user_info = $this->out_cookie();
		if($user_info['group']==10){
			$sql.=" AND a.uid={$user_info['id']}";
		}elseif($user_info['group']==12){
			$sql.=" AND a.uid={$user_info['master']}";
		}else{
			if($data['uid']){
				$sql.=" AND a.uid={$data['uid']}";
			}
		}

		$sql .= " order by a.state asc,a.id desc";
		if(!$page && !$pagesize){
			return $this->_mysql(0)->find($sql);
		}

		return $this->_mysql(0)->findlimit($sql,$page,$pagesize);
	}
	/**
	* 项目作业点列表
	*
	* @param mixed $data
	* @param mixed $page
	* @param mixed $pagesize
	* @return mysql对象
	*/
	public function get_project_address_list($data,$page=1,$pagesize=10){
		$sql = "SELECT a.*,b.title as btitle FROM {$this->table_project_address} as a left join {$this->table_project} as b on a.pid=b.id WHERE 1";
		if($data['title']){//地名或者项目名
			$sql.=" And (a.title like '%{$data['title']}%' or b.title like '%{$data['title']}%')";
		}
		$user_info = $this->out_cookie();
		if($user_info['group']==10){
			$sql.=" AND b.uid='{$user_info['id']}'";
		}
		if($data['pid']){
			$sql.=" AND a.pid='{$data['pid']}'";
		}
		if($data['all']===true){
			return $this->_mysql(0)->find($sql);
		}
		return $this->_mysql(0)->findlimit($sql,$page,$pagesize);
	}
	/**
	* 添加项目设备绑定
	* @author xiaxin 2014-06-10
	* @param mixed $data
	*/
	public function project_device_bind_add($data,$id=0){
		if(!$data)return false;

		//修改
		if($id){
			$where="id={$id}";
			$rst = $this->set_db($this->table_project_bind,$data,false,$where);
			$this->device_state_change($data['did'],2);
			return true;
		}

		$rst = $this->set_db($this->table_project_bind,$data);
		if(!$rst)return false;

		return $this->device_state_change($data['did'],2);
	}


	/**
	 * 查询绑定信息
	 * @param unknown $pid
	 * @param unknown $did
	 */
	public function project_device_bind_find($pid,$did){
		$sql = "SELECT * FROM {$this->table_project_bind} WHERE pid={$pid} AND did={$did} " ;
		return $this->_mysql(0)->findone($sql);
	}


	/**
	 * 查询绑定信息
	 * @param unknown $pid
	 * @param unknown $did
	 */
	public function project_device_group_find($pid,$did,$group,$data=false){
		$sql = "SELECT * FROM {$this->table_record} WHERE type=2 AND state=1 AND pid={$pid} AND did={$did} AND `group`={$group}" ;

        //时间  是拍照的时间
        if($data['start_time']){
            $sql .= " AND ctime>=".(strtotime($data['start_time']));
        }
        if($data['end_time']){
            $sql .= " AND ctime<".(strtotime($data['end_time']));
        }

        //分班时段查询
        if($data['fbtime']){
            $fbrtn=explode("-",$data['fbtime']);
            $sql .=" AND FROM_UNIXTIME(`ctime`,'%H:%i')>='".$fbrtn[0]."' AND FROM_UNIXTIME(`ctime`,'%H:%i')<='".$fbrtn[1]."'";
        }

		return $this->_mysql(0)->find($sql);
	}


	/**
	* 根据设备id检查设备是否已经绑定
	* @author xiaxin 2014-06-10
	* @param int $id
	*/
	public function check_device_bind($id){
		if(!is_numeric($id)){
			return false;
		}
		$sql = "SELECT id FROM {$this->table_device} WHERE id = {$id} AND state=1";
		return $this->_mysql(0)->findone($sql);
	}
	/**
	* 更新或添加作业地址
	*
	* @param mixed $data
	* @return mixed
	*/
	public function address_data_ins_up($data){
		if(!$data) return false;
		return !$data['id']?$this->set_db($this->table_project_address,$data):$this->set_db($this->table_project_address,$data,false,"id={$data['id']}");
	}

	/**
	* 获取项目绑定设备列表
	*
	* @param mixed $data
	* @param mixed $page
	* @param mixed $pagesize
	*/
	public function get_bind_list($data,$page=1,$pagesize=10){
		$sql = "SELECT a.*,b.title,c.no FROM {$this->table_project_bind} as a,{$this->table_project} as b,{$this->table_device} as c WHERE a.pid=b.id and a.did=c.id";
		if($data['pid']){
			$sql .= " AND a.pid in ({$data['pid']})";
		}
		$user_info = $this->out_cookie();
		if($user_info['group']==10){
			$sql .= " And b.uid='{$user_info['id']}'";
		}
		if($data['state']){
			$sql .= " AND a.state={$data['state']}";
		}
		if($data['type']){
			$sql .= " AND a.type={$data['type']}";
		}
		if($data['carno']){
			$sql .=" AND a.car_number LIKE '%{$data['carno']}%'";
		}
		$sql .= " ORDER BY time DESC";

		if(!$page && !$pagesize){
			return $this->_mysql(0)->find($sql);
		}
		return $this->_mysql(0)->findlimit($sql,$page,$pagesize);
	}

	/**
	* 项目关闭（完结）时，取消项目绑定的所有设备。
	* @author xiaxin 2014-06-11
	* @param int $pid 项目的id
	*/
	public function project_remove_all_bind($pid){
		$bind_list = $this->get_bind_list(array('pid'=>$pid,'state'=>1),false,false);
		$ids = '';
		$dids = '';
		if(!$bind_list){
			return false;
		}
		foreach($bind_list as $v){
			$ids .= $v['id'].',';
			$dids .= $v['did'].',';
		}
		$ids = trim($ids,',');
		$dids = trim($dids,',');
		$sql = "UPDATE {$this->table_project_bind} as a,{$this->table_device} as b SET a.state=2,b.state=1 WHERE a.id in ({$ids}) and b.id in ({$dids})";
		return $this->_mysql(0)->query($sql,true);
	}

	/**
	* 取消项目的设备绑定，同时修改设备的绑定状态
	* @author xiaxin 2014-06-11
	* @param mixed $id
	* @param mixed $did
	*/
	public function bind_edit($id,$did){
		if(!is_numeric($id) || !is_numeric($did)){
			return false;
		}
		$sql = "UPDATE {$this->table_project_bind} SET state=2 WHERE id={$id} and did={$did}";
		$rst = $this->_mysql(0)->update($sql);
		if($rst>0){
			$rst = $this->device_state_change($did,1);
		}
		return $rst;
	}


	/**
	 * 删除设备绑定信息
	 * @param int $id
	 * @param int $did
	 * @author leiqianyong 2016-9-30 10:48 AM
	 */
	public function bind_del($id,$did){
		if(!is_numeric($id) || !is_numeric($did)){
			return false;
		}

		$sql = "DELETE FROM {$this->table_project_bind} WHERE id={$id} and did={$did}";
		$rst = $this->_mysql(0)->update($sql);
		if($rst>0){
			$rst = $this->device_state_change($did,1);
		}
		return $rst;
	}


	/**
	 * 查询绑定信息
	 * @param unknown $id
	 * @param string $cols
	 */
	public function bind_info($id,$cols='*'){
		if(!is_numeric($id)){
			return false;
		}

		$sql="SELECT {$cols} FROM {$this->table_project_bind} WHERE id={$id}";
		return $this->_mysql(0)->findone($sql);
	}


	/**
	 * 改变设备的绑定状态
	 * @param int $id
	 * @param int $state
	 */
	public function project_record_state_change($id,$state){
		if(!is_numeric($id) || !is_numeric($state)){
			return false;
		}
		$sql = "UPDATE {$this->table_record} SET state={$state} WHERE id={$id}";
		$rst = $this->_mysql(0)->update($sql);
		return $rst;
	}


	/**
	* 改变设备的绑定状态
	* @author xiaxin 2014-06-11
	* @param int $id
	* @param int $state
	*/
	private function device_state_change($id,$state){
		if(!is_numeric($id) || !is_numeric($state)){
			return false;
		}
		$sql = "UPDATE {$this->table_device} SET state={$state} WHERE id={$id}";
		$rst = $this->_mysql(0)->update($sql);
		return $rst;
	}


	public function project_all(){
		$sql = "SELECT id,title FROM {$this->table_project}";
		$result = $this->_mysql(0)->find($sql);
		if(empty($result))return false;

		$res=array();
		foreach($result as $val){
			$res[$val['id']]=$val['title'];
		}
		return $res;
	}


	public function project_total_record(){
		$sql="SELECT count(*) as sum,pid FROM {$this->table_record} GROUP BY pid";
		$result = $this->_mysql(0)->find($sql);

		if(empty($result))return false;

		foreach($result as $val){
			$res[$val['pid']]=$val['sum'];
		}
		return $res;
	}


	/**
	 * 根据条件查询
	 * @param int $uid  查询项目
	 * @return boolean
	 */
	public function project_by_uid($uid){
		if(!is_numeric($uid))return false;

		$sql="SELECT * FROM {$this->table_project} WHERE uid={$uid} ORDER BY state ASC";
		return $this->_mysql(0)->find($sql);
	}


	public function get_record_date($xid=0,$pid=0,$data=array()){
		if(!is_numeric($xid) || !is_numeric($pid))return false;
		$sql="SELECT FROM_UNIXTIME(ctime,'%Y-%m-%d') AS dates,CASE type WHEN gcat>0 THEN 4 ELSE type END AS type,count(*) AS sum FROM syt_checkin WHERE type>0 AND delimg=0 AND xid={$xid} AND pid={$pid} ";

		if(!$data['admin']){
			$sql .= " AND state=1 " ;
		}

		if($data['guest']){
			$day=5;
			$end_time=strtotime(date('Y-m-d'));
			$start_time=$end_time-86400*$day;

			$sql .= "AND ctime>{$start_time} AND ctime<{$end_time} " ;
		}

		$sql.="GROUP BY FROM_UNIXTIME(ctime,'%Y-%m-%d'),type,gcat ORDER BY ctime DESC ";

		return $this->_mysql(0)->find($sql);
	}


	public function get_records_date($xid=0,$pid=0,$data=array()){
		if(!is_numeric($xid) || !is_numeric($pid))return false;
		$sql="SELECT FROM_UNIXTIME(ctime,'%Y-%m-%d') AS dates,type,count(*) AS sum FROM syt_checkin WHERE delimg=0 AND xid={$xid} AND pid={$pid} ";

		//5天前的数据才能删除
			$day=5;
			$end_time=strtotime(date('Y-m-d'));
			$start_time=$end_time-86400*$day;

			$sql .= "AND ctime<{$start_time} " ;

		$sql.="GROUP BY FROM_UNIXTIME(ctime,'%Y-%m-%d') ORDER BY ctime DESC ";

		return $this->_mysql(0)->find($sql);
	}


	public function get_record($xid=0,$pid=0,$type=1,$date){
		if(!is_numeric($xid) || !is_numeric($pid))return false;
		if(4==$type){
            $sql = "SELECT id,img FROM syt_checkin WHERE delimg=0 AND xid={$xid} AND pid={$pid} AND type=1 AND gcat=1 AND state=1  ";
        }else {
            $sql = "SELECT id,img FROM syt_checkin WHERE delimg=0 AND xid={$xid} AND pid={$pid} AND type={$type} AND state=1  ";
        }

		$start_time=strtotime($date);
		$end_time=$start_time+86400;

		$sql .= "AND ctime>{$start_time} AND ctime<{$end_time}" ;

		return $this->_mysql(0)->find($sql);
	}



	public function get_records($data,$page=1,$pagesize=10){
		if(!$data['date']){
			return false;
		}

		$start_time=strtotime($data['date']);
		$end_time=$start_time+86400;

		$sql="SELECT img FROM {$this->table_record} WHERE xid={$data['uid']} AND pid={$data['pid']} AND ctime>{$start_time} AND ctime<{$end_time}";

		return $this->_mysql(0)->findlimit($sql,$page,$pagesize);
	}


	public function recordinfo($id,$cols='*'){
		if(!is_numeric($id)){
			return false;
		}

		$sql = "SELECT {$cols} FROM {$this->table_record} WHERE id={$id}" ;
		return $this->_mysql(0)->findone($sql);
	}


	/**
	 * 查询项目信息
	 * @param string $cols
	 */
	public function getProjectTime($cols='id,time'){
		$sql = "SELECT {$cols} FROM {$this->table_project}" ;
		$res=$this->_mysql(0)->find($sql);
		if(empty($res))return false;

		foreach($res as $vo){
		   $rtn[$vo['id']]=$vo['time'];
		}

		return $rtn;
	}


}
